package com.example.cslp.controller;

import com.example.cslp.pojo.entity.Course;
import com.example.cslp.pojo.entity.Record;
import com.example.cslp.pojo.entity.User;
import com.example.cslp.service.CourseServiceByYh;
import com.example.cslp.service.RecordService;
import com.example.cslp.service.UserService;
import com.example.cslp.web.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author zcy
 */
@RestController
@RequestMapping("/record")
public class RecordController {
    @Autowired
    RecordService recordService;
    @Autowired
    UserService userService;
    @Autowired
    CourseServiceByYh courseServiceByYh;

    // 添加选课记录
    @CrossOrigin
    @RequestMapping("/addRecord")
    public ResponseEntity<Boolean> addRecord(@RequestParam String courseId, @RequestParam String userId) {
        Record record = new Record();
        record.setCourseId(courseId);
        record.setUserId(userId);
        User user=userService.getUserInfoById(userId);
        Course course=courseServiceByYh.getCourseById(courseId);
        float price=course.getPrice()*course.getDiscount();
        if(user.getPoints()>=price){
            return ResponseEntity.ok(recordService.addRecord(record)&&recordService.payPoints(record)) ;
        }else {
            return ResponseEntity.ok(false);
        }

    }

    // 删除选课记录
    @CrossOrigin
    @DeleteMapping("/delRecord")
    public ResponseEntity<Boolean> delRecord(@RequestParam("u_id") String u_id, @RequestParam("c_id") String c_id) {
        System.out.println("Received u_id: " + u_id + ", c_id: " + c_id);
        return recordService.delRecord(u_id, c_id);
    }


    // 获取用户选课信息
    @CrossOrigin
    @GetMapping("/getRecordByUser")
    public JsonResult getRecordByUser(@RequestParam String name) {
        String msg = "获取课程列表成功!";
        return JsonResult.ok(recordService.getRecordByUser(name), msg);
    }

    //获取用户没有选的课
    @CrossOrigin
    @GetMapping("/getUnRecordByUser")
    public JsonResult getUnRecordByUser(@RequestParam String name) {
        String msg = "获取课程列表成功!";
        return JsonResult.ok(recordService.getUnRecordByUser(name), msg);
    }
}
